<?php

namespace Home\Controller;

use Home\Controller\BaseController;

class TopicController extends BaseController {

    public function _initialize() {
        $userToken = '';
        if (I("user_token")) {
            $userToken = I("user_token");
        }
        //查询库
        $where['user_token'] = $userToken;
        $userTokenInfo = M('Users')->where($where)->find();
        if ($userTokenInfo) {
            $this->user_id = $userTokenInfo['user_id'];
        }
    }

    public function detail() {
        $id = I('id', 0, 'intval');

        if ($id == 0) {
            $this->error('请选择正确的话题');
        }

        $p = I('p', 0, 'intval');
        $size = I('psize', 15, 'intval');
        if ($p) {
            $page = $p * $size;
        }

        $where['topic_id'] = $id;
        $where['is_del'] = 0;

        $info = M('Topic')
                ->field('topic_id,topic_name,topic_pic,topic_content')
                ->where($where)
                ->find();

        if (empty($info)) {
            $this->error('话题不存在或已删除');
        }
        $info['follow_num'] = M('TopicFollow')->where(['topic_id' => $info['topic_id']])->count();
        $info['topic_pic'] = 'https://' . $_SERVER['HTTP_HOST'] . __ROOT__ . $info['topic_pic'];

        $is_follow = M('TopicFollow')->where(['topic_id' => $info['topic_id'], 'user_id' => $this->user_id])->find();

        $info['is_follow'] = 0;
        if ($is_follow) {
            $info['is_follow'] = 1;
        }


        $postWhere['p.topic_id'] = $info['topic_id'];
        $postWhere['p.is_del'] = 0;

        $list = M()->table('ot_post p')
                ->field('p.post_id,p.title,p.post_type,p.topic_id,p.user_id,p.content,p.img_list,p.video_url,p.create_time,u.head_imgurl,u.nick_name,p.is_system')
                ->join('left join ot_users u on p.user_id = u.user_id')
                ->where($postWhere)
                ->order('p.create_time desc')
                ->limit($page, $size)
                ->select();

        $info['post_list'] = [];
        if ($list) {
            $info['post_num'] = M('Post')->where(['topic_id' => $id])->count();
            foreach ($list as &$val) {
                $val = formatPost($val, $this->user_id);
            }
            unset($val);

            $info['post_list'] = $list;
        }

        $this->success('获取成功', $info);
    }

    public function getTopicList() {

        $list = M('Topic')
                ->field('topic_id,topic_name')
                ->where(['is_del' => 0])
                ->select();

        if (empty($list)) {
            $list = [];
        }
        $this->success('获取成功', $list);
    }

    public function followOrCancel() {
        if ($this->user_id == 0) {
            $this->error('请登录', '', 101);
        }

        $topic_id = I('topic_id', 0, 'intval');

        if ($topic_id == 0) {
            $this->error('请选择正确的话题');
        }

        $is_follow = M('TopicFollow')->where(['topic_id' => $topic_id, 'user_id' => $this->user_id])->find();

        $res = 0;
        if ($is_follow) {
            $res = M('TopicFollow')->where(['tfollow_id' => $is_follow['tfollow_id']])->delete();
        } else {
            $data['topic_id'] = $topic_id;
            $data['user_id'] = $this->user_id;
            $res = M('TopicFollow')->add($data);
        }

        if ($res) {
            $this->success();
        } else {
            $this->error();
        }
    }

}
